package com.pride1952.myoptimusx3;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.Telephony;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.pride1952.myoptimusx3.util.NotificationType;

/**
 * Created by qiji on 2015/5/14.
 */
public class NotificationService extends NotificationListenerService{
    public static String TAG = "NotificationListenerService";


    private static final String ALARM_ALERT = "com.android.deskclock.ALARM_ALERT";
    private static final String ALARM_DONE = "com.android.deskclock.ALARM_DONE";

    //PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    private boolean isScreenOn = true;
    private NotificationReceiver notificationReceiver = new NotificationReceiver();

    @Override
    public void onCreate(){
        super.onCreate();
        Log.d(TAG, "NotificationService created.");

        registerReceiver(notificationReceiver, notificationIntentFilter());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "NotificationService started.");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn){
        Log.d(TAG, "id=" + sbn.getId() + " Package Name=" + sbn.getPackageName() +
                " Post time= " + sbn.getPostTime() + " Tag =" + sbn.getTag());
        //微信id为4097
        if(!isScreenOn) if(sbn.getPackageName().equals("com.tencent.mm")) pushNotification(NotificationType.NOTITYPE_WECHAT);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn){
        Log.d(TAG, "onNotificationRemoved");
    }

    @Override
    public void onDestroy(){
        Log.d(TAG, "NotificationService destroyed.");
        unregisterReceiver(notificationReceiver);
    }

    private static IntentFilter notificationIntentFilter(){
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        intentFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
        intentFilter.addAction(ALARM_ALERT);
        intentFilter.addAction(ALARM_DONE);

        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        return intentFilter;
    }

    private class NotificationReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "broadcast get:" + intent.getAction());
            switch (intent.getAction()){
                case android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED:{//todo 测试N5会有两个连续的广播，需优化
                    Log.i(TAG, "new state:" + intent.getStringExtra(TelephonyManager.EXTRA_STATE));
                    switch (intent.getStringExtra(TelephonyManager.EXTRA_STATE)) {
                        case "RINGING": if(!isScreenOn) pushNotification(NotificationType.NOTITYPE_PHONE_START);
                            break;
                        case "IDLE": pushNotification(NotificationType.NOTITYPE_PHONE_END);
                            break;
                        case "OFFHOOK": pushNotification(NotificationType.NOTITYPE_PHONE_END);
                            break;
                        default: break;
                    }
                }
                break;
                case Telephony.Sms.Intents.SMS_RECEIVED_ACTION: if(!isScreenOn) pushNotification(NotificationType.NOTITYPE_TEXT);
                    break;
                case ALARM_ALERT: pushNotification(NotificationType.NOTITYPE_ALARM_ALERT);
                    break;
                case ALARM_DONE: pushNotification(NotificationType.NOTITYPE_ALARM_DONE);

                case BluetoothAdapter.ACTION_STATE_CHANGED: pushOther(intent);
                    break;

                case Intent.ACTION_SCREEN_OFF: isScreenOn = false;
                    break;
                case Intent.ACTION_SCREEN_ON: isScreenOn = true;
                    break;

                default: break;
            }
        }
    }

    private void pushNotification(byte notificationType){
        Log.i(TAG, "push notificaiton: " + String.valueOf(notificationType));

        Intent serviceIntent = new Intent(this, BluetoothLeService.class);
        serviceIntent.setAction("PUSH_NOTIFICATION");
        serviceIntent.putExtra("NOTIFICATION_TYPE", notificationType);
        startService(serviceIntent);
    }

    private void pushOther(Intent intent){
        if(intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)
                && intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0) == BluetoothAdapter.STATE_ON){
            Log.i(TAG, "Bluetooth state ON, push to BluetoothLeService.");
            Intent serviceIntent = new Intent(this, BluetoothLeService.class);
            serviceIntent.setAction("BLUETOOTH_STATE_ON");
            startService(serviceIntent);
        }
    }
}
